<?
	$squadid = $_SERVER["argv"][1];

    if (!$squadid)
    	$squadid = $_GET['s'];

    if (!$squadid)
    	$squadid = 1;

	include "kernel.php";
    include "config2.php";
	include "adw_functions.php";

    $link = ConnectToBase();

    //Obnulenie RANKA u vseh pilotov
    $r = mysql_query("update finalpilots set rank=0, rank_date='".time()."' where squadronid='$squadid'");

    $r = mysql_query("
    	update
        	squadrons
        set
        	pot1=0,
        	pot2=0,
        	pot3=0,
        	pot4=0,
        	pot5=0,
        	pot6=0,
        	pot7=0,
        	pot8=0,
        	pot9=0,
        	pot10=0,
        	pot11=0,
        	pot12=0,
        	pot13=0,
        	pot14=0,
        	pot15=0,
        	pot16=0,
        	pot17=0,
        	pot18=0,
        	pot19=0,
        	pot20=0,
			rankpot=0,
			rgiven1=0,
			rgiven2=0,
		    rgiven3=0,
            rgiven4=0,
            rgiven5=0,
            rgiven6=0,
            rgiven7=0,
            rgiven8=0,
            rgiven9=0,
            rgiven10=0,
            squadpot=0,
            sgiven=0
            where id='$squadid'
            ");

    $r = mysql_query("select * from squadrons where id='$squadid'");
	$res = mysql_fetch_assoc($r);

	$corank = GetCORank ($res[maxpilots]);
    $xorank = GetXORank ($res[maxpilots]);
    $rankpot = GetRankPot ($res[maxpilots]);

    mysql_query("update finalpilots set rank='$corank',rank_date='".time()."' where pilotid='".$res[comander]."'");
    mysql_query("update finalpilots set rank='$xorank',rank_date='".time()."' where pilotid='".$res[xcomader]."'");
    mysql_query("update squadrons set rankpot='$rankpot' where id='".$res[id]."'");


    $r = mysql_query("
    	update
        	squadrons
        set
        	pot1=0,
        	pot2=0,
        	pot3=0,
        	pot4=0,
        	pot5=0,
        	pot6=0,
        	pot7=0,
        	pot8=0,
        	pot9=0,
        	pot10=0,
        	pot11=0,
        	pot12=0,
        	pot13=0,
        	pot14=0,
        	pot15=0,
        	pot16=0,
        	pot17=0,
        	pot18=0,
        	pot19=0,
        	pot20=0
            where id='$squadid'
            ");

    $r = mysql_query("
    	update
        	squadrons
        set
 			fuel = 0,
   			ammo = 0,
   			repair = 0,
   			addfuel = 0,
   			addammo = 0,
   			addrepair = 0,
            dfuel=0,
            dammo=0,
            drepair=0,
			eAir = 0,
			eBom = 0,
			eSAir = 0,
			eTank = 0,
			eCar = 0,
			eArt = 0,
			eAaa = 0,
			eWag = 0,
			eShip = 0,
			fAir = 0,
			fSAir = 0,
			fTank = 0,
			fCar = 0,
			fArt = 0,
			fAaa = 0,
			fWag = 0,
			fShip = 0,
			Wound = 0,
			deaths = 0,
			TKON = 0,
			BAIL = 0,
			CRASHED = 0,
			CAPTURED = 0,
			DIED = 0,
			MIA = 0,
			KICK = 0,
			DIEDISCO = 0,
            sorties=0,
            allsorties=0
            where id='$squadid'
            ");

    $r = mysql_query("
    	update
        	squadrons
        set
        	given1=0,
        	given2=0,
        	given3=0,
        	given4=0,
        	given5=0,
        	given6=0,
        	given7=0,
        	given8=0,
        	given9=0,
        	given10=0,
        	given11=0,
        	given12=0,
        	given13=0,
        	given14=0,
        	given15=0,
        	given16=0,
        	given17=0,
        	given18=0,
        	given19=0,
        	given20=0
            where id='$squadid'
            ");

    $r = mysql_query("
    	update
        	finalpilots
        set
        	award1=0,
        	award2=0,
        	award3=0,
        	award4=0,
        	award5=0,
        	award6=0,
        	award7=0,
        	award8=0,
        	award9=0,
        	award10=0,
        	award11=0,
        	award12=0,
        	award13=0,
        	award14=0,
        	award15=0,
        	award16=0,
        	award17=0,
        	award18=0,
        	award19=0,
        	award20=0,
        	award21=0,
        	award22=0,
        	award23=0,
        	award24=0,
        	award25=0,
        	award26=0,
        	award27=0,
        	award28=0,
        	award29=0,
        	award30=0,
        	award31=0,
        	award32=0,
        	award33=0,
            
        	award1_date=0,
        	award2_date=0,
        	award3_date=0,
        	award4_date=0,
        	award5_date=0,
        	award6_date=0,
        	award7_date=0,
        	award8_date=0,
        	award9_date=0,
        	award10_date=0,
        	award11_date=0,
        	award12_date=0,
        	award13_date=0,
        	award14_date=0,
        	award15_date=0,
        	award16_date=0,
        	award17_date=0,
        	award18_date=0,
        	award19_date=0,
        	award20_date=0,
        	award21_date=0,
        	award22_date=0,
        	award23_date=0,
        	award24_date=0,
        	award25_date=0,
        	award26_date=0,
        	award27_date=0,
        	award28_date=0,
        	award29_date=0,
        	award30_date=0,
        	award31_date=0,
        	award32_date=0,
        	award33_date=0,

            fuel=0,
            addfuel=0,
            dfuel=0,

            ammo=0,
            addammo=0,
            dammo=0,

            repair=0,
            addrepair=0,
            drepair=0

            where squadronid='$squadid'
            ");

	$TPmain->Necho = 0;
	$bd = mktime();
	$d = $bd;

	$allpilots_r = mysql_query("select * from finalpilots where squadronid='$squadid' order by id");

    $i=0;
    $ff=0;
    while ($pilot = mysql_fetch_assoc($allpilots_r))
    {

    	$i++;

        $squadronid =$pilot[squadronid];
    	$pilot = $pilot[pilotid];

     	ob_start();

        echo "PARSING PILOT #$pilot\r\n";

        echo "Update_Pilot_Common_Stat...\r\n";

		UpdatePilotCommonStat ($pilot);

        $updates = mysql_query("update sorties set UpdateSorties='0' where pilotid='$pilot'");

        $sorties = mysql_query("select
        		 								id,
        										UNIX_TIMESTAMP(starttime) as s,
	                                           UNIX_TIMESTAMP(endtime) as e,
	                                           pilotid,

	                                           (eSAir+eTank+eCar+eArt+eAaa+eWag+eShip) as ground,

	                                           eAir-eBom as air,
	                                           eBom as bom,

	                                            addfuel,
	                                            addammo,
	                                            addrepair,
	                                            dfuel,
	                                            dammo,
	                                            drepair,

	                                           eSAir as sair,
	                                           eTank as tank,
	                                           eCar as car,
	                                           eArt as art,
	                                           eAaa as aaa,
	                                           eWag as wag,
	                                           eShip as ship,

	                                           SortieEND as end,
	                                           Wound as wound,
	                                           UpdateSorties as upd

	                       from sorties where pilotid='$pilot' order by id DESC");

    	$SUM = 0;

         while ($sortie = mysql_fetch_assoc($sorties)) {

         		$id = $sortie[id];

                UpdateSquadronCommonStat($id,$squadronid);

	            $s = $sortie[s];
	            $e = $sortie[e];
	            $end = $sortie[end];
	            $upd = $sortie[upd];
	            $tank = $sortie[tank];
	            $sair = $sortie[sair];
	            $art = $sortie[art];
	            $aaa = $sortie[aaa];
	            $car = $sortie[car];
	            $wag = $sortie[wag];
	            $ship = $sortie[ship];
	            $air = $sortie[air]-$sortie[bom];
	            $bom = $sortie[bom];
	            $ground = $sortie[ground];
	            $w = $sortie[wound];

	            if (($end != 'MIA' and $end != 'KICK' and $end != 'DIEDISCO') and ($e - $s >= 15*60 or $end != 'TKON' or $air>0 or $ground>0  or $sortie[addammo]>0 or $sortie[addfuel]>0 or $sortie[addrepair]>0 or $sortie[dfuel]>0 or $sortie[dammo]>0 or $sortie[drepair]>0)) {
					echo "<br>($end != 'MIA' and $end != 'KICK' and $end != 'DIEDISCO') and (".($e - $s)." > 15*60 or $end != 'TKON' or $air>0 or $ground>0  or $sortie[addammo]>0 or $sortie[addfuel]>0 or $sortie[addrepair]>0 or $sortie[dfuel]>0 or $sortie[dammo]>0 or $sortie[drepair]>0)";
	                $fightflight = 1;
                    $ff++;
                }
	            else {
	                $fightflight = 0;
                }

	            $sifd = $sortie[end];
	            if ($sifd == 'DIED')
	                $SUM -= 50;
	            elseif ($sifd=='CAPTURED')
	                $SUM -= 50;
	            elseif ($sifd=='DIEDISCO')
	                $SUM -= 100;
	            elseif ($sifd=='MIA')
	                $SUM -= 25;
	            elseif ($sifd=='BAIL')
	                $SUM -= 25;
	            elseif ($sifd=='CRASHED')
	                $SUM -= 10;

	            if ($sifd=='TKON' and $w)
	                $SUM += 7;

	            if ($sifd=='TKON' and $fightflight)
	                $SUM += 5;

		        $SUM += ($air*40 + $bom*150 + $tank*15 + $art*5 + $aaa*5 + $sair*15 + $wag*1 + $car*1 + $ship*100);
	            $SUM += ($sortie[dfuel]*0.4 + $sortie[dammo]*0.4 + $sortie[drepair]*0.4);
	            $SUM += ($sortie[addfuel]*50 + $sortie[addammo]*50 + $sortie[addrepair]*50);


	            $link = ConnectToBase ();
	            if (!$upd) {
	            	mysql_query("update sorties set UpdateSorties='1' where id='$id'");
	            }

	            if ($fightflight) {
	                mysql_query("update squadrons set sorties=sorties+1 where id='$squadronid'");
	                mysql_query("update squadrons set allsorties=allsorties+1 where id='$squadronid'");
	            }
	            else
	                mysql_query("update squadrons set allsorties=allsorties+1 where id='$squadronid'");


        }

	    UpdateSquadSums($SUM,$pilot);

		$d = mktime() - $d;
    	echo "$i. Done in ".$d."\r\n";
        $d = mktime();

        echo "\r\n";
        ob_end_flush();

    }


    $bd = mktime() - $bd;
    echo "Script done. \r\n Pilots parsed: $i \r\n In $bd seconds\r\n";
    echo "<br>Fight sorties: $ff";
?>